<template>
  <div  style="position: absolute; top: 0;left: 0; right: 0;bottom: 0;background-size: cover;background-image: url(./loginBg.jpg);">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span style="position: relative;left: 12px; font-weight: 900;">管理员登录</span>
      </div>
      <div class="text item">

        <el-form status-icon :model="user" :rules="userRules" label-width="100px" class="demo-ruleForm" ref="userForm">
          <el-form-item prop="phone">
            <el-input style="width: 300px; position: relative; right: 90px;" v-model="user.phone"
                      placeholder="手机号"></el-input>
          </el-form-item>
          <!-- password验证 -->
          <el-form-item prop="password">
            <el-input style="width: 300px; position: relative; right: 90px;" type="password" autocomplete="off"
                      show-password
                      v-model="user.password" placeholder="密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button style="width: 300px; position: relative; right: 90px; top: 10px;" type="primary" @click="login">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>


<script>
import {setToken} from "@/util/token";

export default {
  data() {
    return {
      user: {
        phone: "",
        password: "",
      },
      userRules: {
        phone: [
            {required: true, message: "请输入手机号", trigger: "blur"},
          {min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur"},
        ],
        password: [
          {required: true, message: "请输入密码", trigger: "blur"},
        ],
      },
    }

  },
  methods: {
    login() {
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          window.localStorage.removeItem("cecAdminToken")
          this.axios.post("/admin/login", this.user).then(({data}) => {
            if (data.state == 20000) {
              setToken('cecAdminToken', data.data)
              this.$router.push("/admin")
              this.$message.success('登录成功');
              // location.href("/admin");
              //console.log("返回回来的JWT:")
              //console.log(data.data);
            } else if (data.state == 40800) {
              if (data.message == '用户已失效') {
                this.$message.error("用户已被禁用");
              } else {
                this.$message.error(data.message);
              }
            }
          });
        } else {
          this.$message.error("提交失败");
          return false;
        }
      })
      //console.log(window.localStorage)

    },
  }
}
</script>

<style>
.el-inp {
  width: 300px;
}

.demo-ruleForm {
  margin: 50px auto;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  background: #fff;
  padding: 170px 60px 180px;
  width: 350px;
}

</style>